package com.lims.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Toolkit {
	
	private static final Log log = LogFactory.getLog(Toolkit.class);
	
	/**
	 * 默认属性文件路径 -- 系统配置信息
	 */
	private static final String LIMS_PROPERTY_LOCATION = "/com/lims/properties/lims_config.properties";
	
	/**
	 * 根据key读property
	 * @param propertyKey  属性键名
	 * @return 属性值
	 */
	public static String readProperty(String propertyKey){
		
		return readProperty(null, propertyKey);
	}
	
	/**
	 * 根据key读property
	 * @param propertyPath 指定配置文件路径或名称
	 * @param propertyKey Property Key 属性键名
	 * @return 属性值
	 */
	public static String readProperty(String propertyPath, String propertyKey){
		Properties pros = initProperty(propertyPath);
		return pros.getProperty(propertyKey);
	}
	
	/**
	 * 读取属性文件信息，初始化属性。
	 * @param propertyPath 配置文件路径或名称
	 * @return 属性
	 */
	private static Properties initProperty(String propertyPath){
		Properties pros = new Properties();
		String path = null;
		if(propertyPath == null || propertyPath.trim().length() == 0) {
			path = LIMS_PROPERTY_LOCATION;
		}
		if(!path.startsWith("/")){
			path = "/" + path;
		}
		InputStream input = Toolkit.class.getResourceAsStream(path);
		
		if(input == null) {
			log.debug("lims properties required!");
			return null;
		}
		
		try {
			pros.load(input);
		} catch (IOException e) {
			log.error("unexpected exception has ocurred!");
			e.printStackTrace();
		}
		return pros;
	}
		

	

}
